package com.dragee.modules.officialWebsite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.officialWebsite.dao.PhotoAlbumDetailDao;
import com.dragee.modules.officialWebsite.entity.PhotoAlbumDetailEntity;
import com.dragee.modules.officialWebsite.service.PhotoAlbumDetailService;
import com.dragee.modules.sys.entity.FilesEntity;
import com.dragee.utils.UploadUtil;
import com.dragee.utils.WrapperUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("photoAlbumDetailService")
public class PhotoAlbumDetailServiceImpl extends ServiceImpl<PhotoAlbumDetailDao, PhotoAlbumDetailEntity> implements PhotoAlbumDetailService {


    @Resource
    private PhotoAlbumDetailDao photoAlbumDetailDao;

    private String business = "photoAlbumDetail";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<PhotoAlbumDetailEntity> wrapper =
                WrapperUtil.getWrapper(business, params, PhotoAlbumDetailEntity.class);
        IPage<PhotoAlbumDetailEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        return new PageUtils(page);
    }

    @Override
    public List<PhotoAlbumDetailEntity> getByPhotoId(String photoId) {
        List<PhotoAlbumDetailEntity> datas = list(new QueryWrapper<PhotoAlbumDetailEntity>()
                .eq("photoId", photoId).orderByDesc("sort"));
        if (datas.size() > 0) {
            for (PhotoAlbumDetailEntity record : datas) {
                FilesEntity files = UploadUtil.getFilesEntity(record.getFileId());
                if (files != null) {
                    record.setFileName(files.getFileName());
                    record.setFilePath(files.getFilePath());
                }
            }
        }
        return datas;
    }


}